var roleManagerForm_page={};

roleManagerForm_page.action = {
    initBody:function () {
        roleManagerForm_page.interface.initForm();
        roleManagerForm_page.action.bindEvent();
    },
    bindEvent:function () {
        // 提交按钮的事件
        layui.use(['form'],function () {
            var form = layui.form;
            form.on('submit(saveBtn)', function (data) {
                roleManagerForm_page.action.saveBtnClick();
                return false;
            });
        });
        // 取消按钮的事件
        $("#cancelBtn").click(function () {
            AppComm.utils.iframeCloseSelf();
            return false;
        });
    },
    initData:function(rowId){
        roleManagerForm_page.data.rowId = rowId;
    },
    saveBtnClick:function () {
        var groupIdValue = $("input[name='groupName_select_nodeId']").val();
        var groupNameValue = $('#groupName_select_input_id').val();
        if (groupIdValue==undefined || groupIdValue==null) {
            groupIdValue = $("#parentId").val();
        }
        if ($.isEmpty(groupIdValue)) {
            AppComm.layer.warm('所属组织不能为空。');
            return false;
        }
        var orderId = $("#orderId").val();
        // if ($.isEmpty(orderId)) {
        //     layer.alert('排序号不能为空。', {
        //         icon: 0,
        //         skin: 'layer-ext-moon'
        //     });
        //     return false;
        // } else if (!$.isNumber(orderId)) {
        //     layer.alert('排序号必须为数字。', {
        //         icon: 0,
        //         skin: 'layer-ext-moon'
        //     });
        //     return false;
        // }

        AppComm.ajax.post('/roleManager/ajax/saveRole', {
            id: $("#id").val(),
            roleName: $("#roleName").val(),
            comment: $("#comment").val(),
            groupId: groupIdValue,
            orderId: orderId,
            groupName: groupNameValue
        }, function (resultData) {
            parent.AppComm.ajax.onSuccess(resultData);
            AppComm.utils.iframeCloseSelf();
            parent.roleManager_page.action.initBody();
        });
    }
};

roleManagerForm_page.interface = {
    initForm:function () {
        if (roleManagerForm_page.data.rowId == undefined || '' == roleManagerForm_page.data.rowId.trim()) {
            // 添加
            var html = template("roleManagerForm_template");
            $(".roleManagerForm_box").html(html);
            roleManagerForm_page.interface.initGroupDtree();
            roleManagerForm_page.action.bindEvent();

        } else {
            // 编辑
            AppComm.ajax.post('/roleManager/ajax/getRoleById', {
                id: roleManagerForm_page.data.rowId
            }, function (resultData) {
                var html = template("roleManagerForm_template", resultData.data);
                $(".roleManagerForm_box").html(html);
                roleManagerForm_page.interface.initGroupDtree(resultData.data.groupId);
                roleManagerForm_page.action.bindEvent();
            });
        }

    },
    initGroupDtree:function (defaultValue) {
        AppComm.ajax.post("/groupManager/ajax/getGroupList4Dtree", null, function (resultData) {
            if (resultData.code==Enums.ResultEnum().SUCCESS.code) {
                layui.config({
                    base: '/static/comm/plugins/dtree/'
                }).use(['dtree','layer','jquery'],function(){
                    var dtree = layui.dtree, layer = layui.layer, $ = layui.jquery;
                    dtree.render({
                        elem: "#groupName",
                        width: "100%",
                        height: "50",
                        skin: "layui",
                        dataFormat: "list",
                        data: resultData.data,
                        selectTips: "请选择组织",
                        select: true, //指定下拉树模式
                        selectInitVal:defaultValue
                    });
                    roleManagerForm_page.data.dtreeIns = dtree;
                });
            }
        });
    }
};

roleManagerForm_page.data = {
    'rowId':undefined,
    'dtreeIns':undefined,
};